<template>

<div>
  <!-- 提示 -->
      <el-alert title="" type="info" show-icon :closable="false" style="margin-bottom:20px;">
        <span class="el-alert-title">数据一共 {{ total }} 条</span>
      </el-alert>

  <el-table :data="choiceList" style="width: 100%" >
    <el-table-column prop="number" label="试题编号" width="120"> </el-table-column>
    <el-table-column prop="subject" label="学科" width="80"> </el-table-column>
    <el-table-column prop="catalog" label="目录" width="80"> </el-table-column>
    <el-table-column prop="questionType" label="题型" width="80">
      <template slot-scope="scope">
        {{ scope.row.questionType | questionTypeFilter }}
      </template>
    </el-table-column>
    <el-table-column prop="question" label="题干" width="280"> </el-table-column>
    <el-table-column prop="addDate" label="录入时间" width="160"> </el-table-column>
    <el-table-column label="难度" width="80">
      <template slot-scope="scope">
        {{ scope.row.difficulty | difficultyFilter }}
      </template>
    </el-table-column>
    <el-table-column prop="creator" label="录入人" width="100"> </el-table-column>
    <el-table-column label="审核状态" width="100">
      <template slot-scope="scope">
        {{ scope.row.chkState | chkStateFilter }}
      </template>
    </el-table-column>
    <el-table-column prop="chkRemarks" label="审核意见" width="100"> </el-table-column>
    <el-table-column prop="chkUser" label="审核人" width="100"> </el-table-column>
    <el-table-column prop="publishState" label="发布状态" width="100">
      <template slot-scope="scope">
        {{ scope.row.publishState | publishStatusFilter }}
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="200">
      <template slot-scope="scope">
        <el-button type="text" size="small" @click="previewQuestion(scope.row)">预览</el-button>
        <el-button type="text" size="small" :disabled="scope.row.chkState !==0" @click="checkQuestion(scope.row.id)">审核</el-button>
        <el-button type="text" size="small" :disabled="scope.row.publishState ===1" @click="jumpEdit(scope.row.id)">修改</el-button>
        <el-button type="text" size="small"  @click="updateState(scope.row)"><span v-if="scope.row.publishState ===1">下架</span> <span v-else>上架</span></el-button>
        <el-button type="text" size="small" :disabled="scope.row.publishState ===1" @click="delQuesqion(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

<QuestionPreview :questionsDetail="questionsDetail" :isShow="dialogVisible" @close="changedialogVisible"></QuestionPreview>

<QuestionCheck :dialogVisible="isShow" :id="id" @close="closeCheck"></QuestionCheck>
  </div>
</template>

<script>
import QuestionPreview from './questions-preview.vue'
import QuestionCheck from './questions-check.vue'
import { html2Text } from '@/utils/index'
import { detail, remove, choicePublish } from '@/api/hmmm/questions'
export default {
  components: {
    QuestionPreview,
    QuestionCheck
  },
  props: {
    choiceList: {
      type: Array,
      default: () => []
    },
    total: {
      type: Number,
      default: 0
    }

  },

  data () {
    return {
      questionsDetail: {},
      dialogVisible: false,
      isShow: false,
      id: 0
    }
  },
  created () {

  },
  methods: {
    // 获取题目详情
    async previewQuestion (data) {
      const res = await detail({ id: data.id })
      console.log(res.data)
      this.questionsDetail = res.data
      this.questionsDetail.question = html2Text(this.questionsDetail.question)
      this.questionsDetail.answer = html2Text(this.questionsDetail.answer)
      this.dialogVisible = true
    },
    // 关闭预览
    changedialogVisible (val) {
      this.dialogVisible = val
    },
    // 关闭审核弹窗
    closeCheck (val) {
      this.isShow = val
      this.$emit('check')
    },
    // 审核
    checkQuestion (id) {
      this.isShow = true
      this.id = id
      console.log(this.id)
    },
    // 删除
    delQuesqion (data) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          console.log(data)
          const res = await remove(data)
          if (res.data.success) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.$emit('delete')
          } else {
            this.$message({
              type: 'error',
              message: '删除失败!'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 上下架

    async updateState (row) {
      if (row.publishState === 1) {
        this.$confirm('您确认下架这道题目吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            const res = await choicePublish({ id: row.id, publishState: 0 })
            if (res.data.success) {
              this.$message({
                type: 'success',
                message: '下架成功!'
              })
              this.$emit('update')
            } else {
              this.$message({
                type: 'error',
                message: '下架失败!'
              })
            }
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消此操作'
            })
          })
      } else {
        this.$confirm('您确认上架这道题目吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            const res = await choicePublish({ id: row.id, publishState: 1 })
            if (res.data.success) {
              this.$message({
                type: 'success',
                message: '上架成功!'
              })
              this.$emit('update')
            } else {
              this.$message({
                type: 'error',
                message: '上架失败!'
              })
            }
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消此操作'
            })
          })
      }
    },
    // 跳转编辑
    jumpEdit (id) {
      this.$router.push({ name: 'questions-new', query: { id: id } })
    }
  }

}
</script>

<style lang="scss" scoped>
::v-deep .el-alert__icon.is-big {
  font-size: 16px;
  width: 16px;
}
::v-deep .el-alert__description {
  margin-top: 0;
}
.el-alert-title {
  font-size: 13px;
  line-height: 18px;
}
</style>
